import { useModel, history } from '@umijs/max'
import { PLATFORM_URL } from '@/constants'
import { Space, Card, Image } from 'antd'
import styles from './index.less'
import { useEffect, useState } from 'react'
import { setCookie, getDomain, crypt } from '@/utils/utils'

export default () => {
  const { platformType } = useModel('Global')
  const [loginPlatformList, setLoginPlatformList] = useState([])

  const goTo = data => {
    const a = JSON.stringify(data)
    const coa = crypt.encode(a)
    setCookie('roleList', coa, getDomain(), true)
    setCookie('autoL', true, getDomain(), true)
    history.push(`/main/rolePicker`)
  }

  useEffect(() => {
    if (platformType) {
      window.location.href = `${PLATFORM_URL[0]}${window.location.pathname}`
    } else {
      try {
        const list = JSON.parse(localStorage.getItem('loginPlatformList'))
        if (list && list.length) {
          setLoginPlatformList(list)
        } else {
          history.push('/main/specialPortal')
        }
      } catch (error) {
        console.log('check platform err', error)
        history.push('/main/specialPortal')
      }
    }
  }, [])

  return (
    <div className={styles.platform_picker_wrapper}>
      <Space wrap size='large'>
        {loginPlatformList
          ? loginPlatformList.map((i, index) => (
              <Card className={styles.platform_card} key={index} onClick={() => goTo(i.data)}>
                <Space>
                  <Image width={50} preview={false} src={i.logo} alt='' />
                  <div>{i.name}</div>
                </Space>
              </Card>
            ))
          : null}
      </Space>
    </div>
  )
}
